package com.sky.wonderrelationship.entity;

import com.github.pagehelper.util.StringUtil;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;

/**
 * 表到表之间的关系树
 *
 * @author: lx
 * @date: 2023年04月19日 8:48
 */
public class TreeRelationshipsNode {

    //当前节点信息
    private Long id;
    private String db;
    private String table;
    private String field;

    //是否属于满足条件的有效节点（不然递归时会把关于有用的父节点给删掉）
    private Boolean valid = false;

    //是否被标记为删除状态
    private Boolean delete;
    //子节点list
    private List<TreeRelationshipsNode> treeRelationshipsNodeList = new ArrayList<>();

    public TreeRelationshipsNode( Long id, String db, String table, String field){
        this.id = id;
        this.db = db;
        this.table = table;
        this.field = field;
    }


    public TreeRelationshipsNode(){}

    //添加成功返回true
    public Boolean addChild(TreeRelationshipsNode child,Set<Long> fatherIdSet){
        if(fatherIdSet.contains(child.id)){
            return false;
        }
        //父节点中不存在子节点，那就加入子节点
        this.treeRelationshipsNodeList.add(child);
        return true;

    }

    // 遍历所有叶子节点，如果不满足条件，则去除该叶子节点
    //如果叶子节点满足要求，就返回true 它的父节点们就不要进行判断了，不然父节点被删了，它也没了
    public Boolean makeTagInvalidNodes(TreeRelationshipsNode node, Field field,Boolean leafIsValid) {
        if (node == null) {
            return false;
        }
        for (int i = 0; i < node.treeRelationshipsNodeList.size(); i++) {
            TreeRelationshipsNode child = node.treeRelationshipsNodeList.get(i);
            leafIsValid = makeTagInvalidNodes(child,field,leafIsValid);
            if(leafIsValid == false){
                child.setValid(child.getValid()==true?true:isValid(child, field));
                //要排除一种情况，就是能直接到达子节点的，那就要改变它的父节点的值
                if(child.getValid() == true){
                    node.setValid(true);
                }
                return child.getValid();
            } else {
                //子节点有用，那父节点也得有用
                node.setValid(true);
                child.setValid(true);
                return true;
            }
        }
        return false;
    }

    public boolean isValid(TreeRelationshipsNode node,Field  field) {
        // 这里可以根据需要实现自定义的判断条件
        // 如果满足条件，返回true，否则返回false
        if (node.getDb().equals(field.getDb()) && node.getTable().equals(field.getTable())) {
            return true;
        } else {
            return false;
        }
    }



    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("当前节点为:");
        builder.append(this.id);
        builder.append(",children=[");
        for (int i = 0; i < treeRelationshipsNodeList.size(); i++) {
            builder.append(treeRelationshipsNodeList.get(i).db)
                    .append(".")
                    .append(treeRelationshipsNodeList.get(i).table)
                    .append(".")
                    .append(treeRelationshipsNodeList.get(i).field);
            if (i < treeRelationshipsNodeList.size() - 1) {
                builder.append(", ");
            }
        }
        builder.append("])");
        return builder.toString();
    }



    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDb() {
        return db;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public String getField() {
        return field;
    }

    public void setField(String field) {
        this.field = field;
    }

    public List<TreeRelationshipsNode> getTreeRelationshipsNodeList() {
        return treeRelationshipsNodeList;
    }

    public void setTreeRelationshipsNodeList(List<TreeRelationshipsNode> treeRelationshipsNodeList) {
        this.treeRelationshipsNodeList = treeRelationshipsNodeList;
    }

    public Boolean getValid() {
        return valid;
    }

    public void setValid(Boolean valid) {
        this.valid = valid;
    }

    public Boolean getDelete() {
        return delete;
    }

    public void setDelete(Boolean delete) {
        this.delete = delete;
    }
}
